﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BLL;
using Exceptions;

namespace WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WCFTransporte" in code, svc and config file together.
    public class WCFTransporte : IWCFTransporte
    {
        public List<DTO.Transporte> list()
        {
            BLLTransporte bllTransportes = new BLLTransporte();
            List<BTO.Transporte> btoTransportes = bllTransportes.list();
            List<DTO.Transporte> dtoTransportes = new List<DTO.Transporte>();
            DTO.Transporte dtoTransporte;

            foreach (BTO.Transporte btoTransporte in btoTransportes)
            {
                dtoTransporte = new DTO.Transporte();
                dtoTransporte.IDTransporte = btoTransporte.IDTransporte;
                dtoTransporte.IDTipoTransporte = btoTransporte.IDTipoTransporte;
                dtoTransporte.IDPartida = btoTransporte.IDPartida;
                dtoTransporte.IDChegada = btoTransporte.IDChegada;
                dtoTransporte.lotacao = btoTransporte.lotacao;

                dtoTransportes.Add(dtoTransporte);
            }

            return dtoTransportes;
        }

        public DTO.Transporte getByID(int id)
        {
            BLLTransporte bllTransportes = new BLLTransporte();
            DTO.Transporte dtoTransporte = new DTO.Transporte();

            try
            {
                BTO.Transporte btoTransporte = bllTransportes.getByID(id);

                dtoTransporte.IDTransporte = btoTransporte.IDTransporte;
                dtoTransporte.IDTipoTransporte = btoTransporte.IDTipoTransporte;
                dtoTransporte.IDPartida = btoTransporte.IDPartida;
                dtoTransporte.IDChegada = btoTransporte.IDChegada;
                dtoTransporte.lotacao = btoTransporte.lotacao;

            }
            catch (NotFoundTranporteException e)
            {
                throw new NotFoundTranporteException(e.Message);
            }
            catch (Exception)
            {
                throw new Exception("Transporte não encontrado!");
            }

            return dtoTransporte;
        }

        public List<DTO.Transporte> getByTipoTransporte(int idTipoTransporte)
        {
            BLLTransporte bllTransportes = new BLLTransporte();
            List<BTO.Transporte> btoTransportes = bllTransportes.getByTipoTransporte(idTipoTransporte);
            List<DTO.Transporte> dtoTransportes = new List<DTO.Transporte>();
            DTO.Transporte dtoTransporte;

            foreach (BTO.Transporte btoTransporte in btoTransportes)
            {
                dtoTransporte = new DTO.Transporte();
                dtoTransporte.IDTransporte = btoTransporte.IDTransporte;
                dtoTransporte.IDTipoTransporte = btoTransporte.IDTipoTransporte;
                dtoTransporte.IDPartida = btoTransporte.IDPartida;
                dtoTransporte.IDChegada = btoTransporte.IDChegada;
                dtoTransporte.lotacao = btoTransporte.lotacao;

                dtoTransportes.Add(dtoTransporte);
            }

            return dtoTransportes;
        }
    }
}
